package mydessertstore.web.servlet;

import mydessertstore.domain.Item;
import mydessertstore.domain.Product;
import mydessertstore.service.CatalogService;
import mydessertstore.domain.Account;
import mydessertstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ItemFormServlet extends HttpServlet {



    private CatalogService catalogService;

    private static final String ITEM_FORM = "/WEB-INF/jsp/catalog/item.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset = UTF-8");

        String itemId = req.getParameter("itemId");
        System.out.println("传到item页面的"+itemId);
        catalogService = new CatalogService();
        Item item = catalogService.getItem(itemId);
        Product product = item.getProduct();

        HttpSession session = req.getSession();
        session.setAttribute("product", product);
        session.setAttribute("item", item);

        //日志功能
        Account account;
        account=(Account) session.getAttribute("account");
        if(account!=null) {
            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + " 查看了"+itemId ;
            logService.insertLogInfo(account.getUsername(), logInfo);
        } else{
            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + " 查看了"+itemId ;
            logService.insertLogInfo("未登录用户", logInfo);
        }
        //日志结束

        req.getRequestDispatcher(ITEM_FORM).forward(req, resp);
    }
}
